<?php


namespace app\common\aliyun;

class Sms
{
	public $accessKeyId = "";
	public $accessSecret = "";
	public $signName = "";
	public $config = [];
	public function __construct($config = [])
	{
		if (!$config) {
			$config = \app\common\server\ConfigServer::get("sms", "aliyun");
		}
		$this->config = $config;
		$this->accessKeyId = $config["accessKeyId"];
		$this->accessSecret = $config["accessSecret"];
		$this->signName = $config["signName"];
	}
	public function send($phoneNumber, $templateCode, $data)
	{
		try {
			$config = new \Darabonba\OpenApi\Models\Config(["accessKeyId" => $this->accessKeyId, "accessKeySecret" => $this->accessSecret]);
			$config->endpoint = "dysmsapi.aliyuncs.com";
			$client = new \AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi($config);
			$sendSmsRequest = new \AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest(["phoneNumbers" => $phoneNumber, "signName" => $this->signName, "templateCode" => $templateCode, "templateParam" => json_encode($data)]);
			$res = $client->sendSms($sendSmsRequest);
			if ($res->body->code == "OK") {
				return true;
			} else {
				return $res->body->message;
			}
		} catch (\Exception $e) {
			return $e->getMessage();
		}
	}
	public function code($phone, $code)
	{
		return $this->send($phone, $this->config["template_code"]["verification_code"], ["code" => $code]);
	}
	public function card_buy_deliver($phone, $order_no, $card)
	{
		return $this->send($phone, $this->config["template_code"]["card_buy_deliver"], ["order" => $order_no, "card" => $card]);
	}
}